const Nedb = require('nedb')

export const nedb = new Nedb({
  filename: '/static/redis.db',
  autoload: true
})

export const saveRedisData = (doc, callback) => {
  if (doc._id !== null && doc._id !== '') {
    // eslint-disable-next-line no-unused-vars
    doc.database = 'redis'
    doc.table = 'configure'
    // eslint-disable-next-line handle-callback-err
    return nedb.insert(doc, (err, newDoc) => {
      callback(newDoc)
    })
  } else {
    // eslint-disable-next-line handle-callback-err
    return nedb.update({_id: doc._id}, doc, (err, newDoc) => {
      callback(newDoc)
    })
  }
}

export const insertRedisData = (doc, callback) => {
  doc.database = 'redis'
  doc.table = 'configure'
  // eslint-disable-next-line handle-callback-err
  return nedb.insert(doc, (err, newDoc) => {
    callback(newDoc)
  })
}

export const updateRedisData = (doc, callback) => {
  // eslint-disable-next-line handle-callback-err
  return nedb.update({_id: doc._id}, doc, (err, newDoc) => {
    callback(newDoc)
  })
}

export const deleteRedisData = (doc, callback) => {
  // eslint-disable-next-line handle-callback-err
  return nedb.remove(doc, (err, docs) => {
    callback(docs)
  })
}

export const listRedisData = (doc, callback) => {
  // eslint-disable-next-line handle-callback-err
  return nedb.find(doc, (err, docs) => {
    callback(docs)
  })
}
